/*
 * CycleTargetArea.cpp
 *
 *  Created on: 2010-mar-15
 *      Author: he huang
 */

#include "CycleTargetArea.h"
#include "Coordinates.h"
#include "Split.h"

#include <cmath>

using namespace std;

CycleTargetArea::CycleTargetArea()
{
	this->_center = new Coordinates();
	this->_radius = 0;
}

CycleTargetArea::CycleTargetArea(const Coordinates & center, double radius)
{
	this->_center = new Coordinates(center);
	this->_radius = radius;
}

CycleTargetArea::CycleTargetArea(const CycleTargetArea & area)
{
	this->_center = new Coordinates(area.getCenter());
	this->_radius = area.getRadius();
}

CycleTargetArea::~CycleTargetArea()
{
	if(_center != NULL){
		delete _center;
		_center = NULL;
	}
}

/*
 * check on which side of the split is the target area located
 */
Side CycleTargetArea::onWhichSide(const Split & split)
{
	double dis = 0;
	if(split._dimension == X)
		dis = this->_center->x - split._coordinate;
	else
		dis = this->_center->y - split._coordinate;

	if(fabs(dis) < _radius) // overlaps with splitting
		return BOTH;
	else if(dis < 0) // on decreasing side
		return DEC;
	else			// on increasing side
		return INC;
}

int CycleTargetArea::sizeInBits()
{
	return 3 * sizeof(double);
}

CycleTargetArea & CycleTargetArea::operator =(const CycleTargetArea & other)
{
	this->_center->x = other._center->x;
	this->_center->y = other._center->y;
	this->_radius = other._radius;

	return *this;
}



std::ostream& operator<<(std::ostream& Stream, const CycleTargetArea& v)
{
    return Stream << "center" << *(v._center) << " "<< "radius " << v._radius << endl;
}

void CycleTargetArea::netPack(cCommBuffer *b)
{
    //cMessage::netPack(b);
    doPacking(b, *(this->_center));
    doPacking(b, this->_radius);
}

void CycleTargetArea::netUnpack(cCommBuffer *b)
{
    //cMessage::netUnpack(b);
    doUnpacking(b, *(this->_center));
    doUnpacking(b, this->_radius);
}













